﻿using System.Collections.Generic;
using System.Web;
using FriendlyURL.Web.Domain.Abstract;

namespace FriendlyURL.Web.Domain.Impl
{
    public class WebContext : IWebContext
    {
        /// <summary>
        /// UserCleanNetworkID
        /// <remarks>
        /// Used to store a cleaned version of a user's network id without the domain name
        /// </remarks>
        /// </summary>
        public string UserCleanNetworkID
        {
            get
            {
                if (ContainsInSession(GlobalProperties.USER_CLEAN_NETWORKID))
                {
                    return GetFromSession(GlobalProperties.USER_CLEAN_NETWORKID) as string;
                }

                return string.Empty;
            }
            set
            {
                SetInSession(GlobalProperties.USER_CLEAN_NETWORKID, value);
            }
        }

        /// <summary>
        /// UserExists
        /// <remarks>
        /// Used to store a cleaned version of a user's network id without the domain name
        /// </remarks>
        /// </summary>
        public bool UserExists
        {
            get
            {
                if (ContainsInSession(GlobalProperties.USER_EXISTS))
                {
                    return GetFromSession(GlobalProperties.USER_EXISTS).Equals(GlobalProperties.CONST_YES_VALUE);
                }

                return false;
            }
            set
            {
                if (value.Equals(true))
                {
                    SetInSession(GlobalProperties.USER_EXISTS, GlobalProperties.CONST_YES_VALUE);
                }
                else
                {
                    SetInSession(GlobalProperties.USER_EXISTS, GlobalProperties.CONST_NO_VALUE);
                }
            }
        }

        public List<string> UserRoles
        {
            get
            {
                if (ContainsInSession(GlobalProperties.USER_ROLES))
                {
                    return GetFromSession(GlobalProperties.USER_ROLES) as List<string>;
                }

                return null;
            }

            set
            {
                SetInSession(GlobalProperties.USER_ROLES, value);
            }
        }

        public void ClearSession()
        {
            HttpContext.Current.Session.Clear();
        }

        public bool ContainsInSession(string key)
        {
            if (HttpContext.Current.Session != null && HttpContext.Current.Session[key] != null)
                return true;
            return false;
        }

        public void RemoveFromSession(string key)
        {
            HttpContext.Current.Session.Remove(key);
        }


        #region Private Methods

        private string GetQueryStringValue(string key)
        {
            return HttpContext.Current.Request.QueryString.Get(key);
        }

        private void SetInSession(string key, object value)
        {
            if (HttpContext.Current == null || HttpContext.Current.Session == null)
            {
                return;
            }
            HttpContext.Current.Session[key] = value;
        }

        private object GetFromSession(string key)
        {
            if (HttpContext.Current == null || HttpContext.Current.Session == null)
            {
                return null;
            }
            return HttpContext.Current.Session[key];
        }

        private void UpdateInSession(string key, object value)
        {
            HttpContext.Current.Session[key] = value;
        }

        #endregion
    }
}
